package cn.ggy.web;

import cn.ggy.polo.Student;
import cn.ggy.service.StuService;
import cn.ggy.service.StuServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/helloServlet")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 处理post请求乱码问题
        request.setCharacterEncoding("utf-8");
        //设置相应数据的编码
        response.setContentType("text/html;charset=utf-8");

        StuService stuService = new StuServiceImpl();
        //获取数据并将数据放在作用域中
        List<Student> stus = stuService.findStus();
        request.setAttribute("stu",stus);
        request.getRequestDispatcher("hello.jsp").forward(request,response);
        for (Student student : stus) {
            System.out.println(student);
        }
        response.getWriter().write(123);
        System.out.println(456);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);
    }
}